package com.hnkywl.sts.dao.tjbb;

import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import oracle.jdbc.OracleCallableStatement;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.Date;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-10-18
 * Time: 上午11:13
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class YdcbdbDao extends SimpleHibernateTemplate {
    /**
     * 移动成本对比表
     * @param gsid
     * @param begindt
     * @param enddt
     * @param jylb
     * @param xslx
     * @param custname
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Map<String, Object> ydcbdbList(Integer gsid,   Date begindt, Date enddt,Integer jylb,Integer xslx, String custname,
                                         Integer pageNo, Integer pageSize) {
        //1、gs_id 若为空  则等于 -1
        if (gsid == null)
            gsid = -1;
        Integer v_gsid;
        v_gsid = gsid;
        //1jylb 若为空  则等于 -1
        if (jylb == null)
            jylb = -1;
        Integer v_jylb;
        v_jylb = jylb;
        //1、xslx 若为空  则等于 -1
        if (xslx == null)
            xslx = -1;
        Integer v_xslx;
        v_xslx = xslx;
        //得到月初
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(enddt);
        calendar.add(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        java.sql.Date paramDate = new java.sql.Date(begindt.getTime());
        java.sql.Date paramDate1 = new java.sql.Date(calendar.getTime().getTime());

      if (custname == null || "".equals(custname))
          custname = "-1";
        Connection con = null;
        CallableStatement call = null;
        ResultSet rs = null;
        Integer total = 0;
        try {
            con = getSession().connection();
            call = con.prepareCall("{Call P_RPT_YDCBDB(?,?,?,?,?,?,?,?,?,?)}");
            call.setInt(1, gsid);
            call.setDate(2, paramDate);
            call.setDate(3, paramDate1);
            call.setInt(4, jylb);
            call.setInt(5, xslx);
            call.setString(6, custname);
            call.setInt(7, (pageNo - 1) * pageSize);
            call.setInt(8, pageNo * pageSize);
            call.registerOutParameter(9, Types.NUMERIC);
            call.registerOutParameter(10, oracle.jdbc.OracleTypes.CURSOR);
            call.execute();
            total = call.getInt(9);
            rs = ((OracleCallableStatement) call).getCursor(10);
            ResultSetMetaData resultsMetaData = rs.getMetaData();
            int columnCount = resultsMetaData.getColumnCount();
            Map<String, Object> result = new HashMap<String, Object>();
            List<Map> rows = new ArrayList<Map>();
            List<Map> footer = new ArrayList<Map>();
            while (rs.next()) {
                Map<String, Object> r = new HashMap<String, Object>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = resultsMetaData.getColumnName(i).toLowerCase();
                    r.put(columnName, rs.getObject(i));
                }
                if (!"合计".equals(r.get("gs_name").toString()))
                    rows.add(r);
                else
                    footer.add(r);
            }
            result.put("total", total);
            result.put("rows", rows);
            result.put("footer", footer);
            return result;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        } finally {
            closeResource(rs, call, con);
        }
    }
    private void closeResource(ResultSet rs, Statement s, Connection con) {
        if (rs != null)
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (s != null)
            try {
                s.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
        if (con != null)
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
    }

}
